package top.mable.rabbitmq.listener;

import cn.hutool.core.util.ObjectUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import top.mable.rabbitmq.dto.MqMsg;

import java.io.IOException;

/**
 * @author taffywen
 * @date 2022/7/24 11:11
 */

@Component
@Slf4j
public class PrintListener {

//    @RabbitListener(queues = "printQueue")
    @RabbitListener(bindings = {@QueueBinding(
        exchange = @Exchange(value = "rabbitMqExchange",type = ExchangeTypes.TOPIC),
        value = @Queue(value = "printQueue",durable = Exchange.TRUE,autoDelete = Exchange.FALSE),
        key = "print")})
    public void doPrint(Message message, Channel channel) throws IOException {
        MqMsg unserialize = ObjectUtil.unserialize(message.getBody());
        log.info("消息消费，内容为{}",unserialize.toString());

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
